package com.wing.oauth2.details.userdetails.user;

import cn.hutool.core.collection.CollectionUtil;
import com.wing.common.constant.GlobalConstants;
import com.wing.user.model.response.UserAuthVO;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;


/**
 * 系统管理用户认证信息
 *
 * @author
 */
@Data
public class SysUserDetails implements UserDetails {

    /**
     * 扩展字段
     */
    private Long userId;
    private String authMethod;

    /**
     * 默认字段
     */
    private String username;
    private String password;
    private Boolean enabled;
    private Collection<SimpleGrantedAuthority> authorities;

    private String nickname;
    private String enterpriseCode;

    private String enterpriseId;

    private String enterpriseName;

    private Long departmentId;
    //添加岗位id
    private Long postId;

    private String departmentName;

    private String phone;

    private String cabAuthCodes;

    private String roleName;

    private String wcabAuthCodes;

    private String roleCode;

    private String channelCode;

    private Long roleId;

    /**
     * 公司信息
     */
    private Long companyInfoId;

    private String costCenter;

    /**
     * 系统管理用户
     */
    public SysUserDetails(UserAuthVO user) {
        this.setUserId(user.getUserId());
        this.setUsername(user.getUsername());
        this.setPassword(user.getPassword());
        this.setNickname(user.getNickname());
        this.setEnterpriseCode(user.getEnterpriseCode());
        this.setEnterpriseId(user.getEnterpriseId());
        this.setEnterpriseName(user.getEnterpriseName());
        this.setEnabled(GlobalConstants.STATUS_YES.equals(user.getStatus()));
        this.setDepartmentId(user.getDepartmentId());
        this.setPostId(user.getPostId());
        this.setDepartmentName(user.getDepartmentName());
        this.setPhone(user.getPhone());
        this.setRoleName(user.getRoleName());
        this.setCabAuthCodes(user.getCabAuthCodes());
        this.setWcabAuthCodes(user.getWcabAuthCodes());
        this.setRoleCode(user.getRoleCode());
        this.setChannelCode(user.getChannelCode());
        this.setRoleId(user.getRoleId());
        this.setCostCenter(user.getCostCenter());
        if (CollectionUtil.isNotEmpty(user.getRoles())) {
            authorities = new ArrayList<>();
            user.getRoles().forEach(role -> authorities.add(new SimpleGrantedAuthority(role)));
        }
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return this.authorities;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    public String getEnterpriseCode() {
        return this.enterpriseCode;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return this.enabled;
    }

}
